﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using BrnMall.WeiXin.Sdk.Domain;
using Newtonsoft.Json;
using WeiTap.Web;

namespace BrnMall.WeiXin.Sdk
{
    public static class SendTemplateMessage
    {
        /// <summary>
        /// 发送模板消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        internal static void PostTemplateMessage(string accessToken, TemplateMessage message)
        {
            try
            {
                var url = string.Format(
                    "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={0}",
                    accessToken);
 
                var request = (HttpWebRequest)WebRequest.Create(url);
                var postData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = postData.Length;
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(postData, 0, postData.Length);
                }
                var response = (HttpWebResponse)request.GetResponse();
                var resStream = response.GetResponseStream();
                if (resStream == null) return;
                var wwwresult = new StreamReader(resStream).ReadToEnd();
                Trace.TraceInformation("返回结果:" + wwwresult);
                
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }

        /// <summary>
        /// 发货成功消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openid"></param>
        /// <param name="templateId"></param>
        /// <param name="prepayId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static void PostDeliverSuccess(string accessToken, string openid, string templateId, string prepayId, TemplateData data)
        {
            try
            {
                var message = new TemplateMessage
                {
                    touser = openid,
                    template_id = templateId,
                    page = "pages/index/index",
                    form_id = prepayId,
                    data = data
                };
                PostTemplateMessage(accessToken, message);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }
        /// <summary>
        /// 支付成功消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openid"></param>
        /// <param name="templateId"></param>
        /// <param name="prepayId"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static void PostPaySuccess(string accessToken, string openid, string templateId, string prepayId, TemplateData data)
        {
            try
            {
                var message = new TemplateMessage
                {
                    touser = openid,
                    template_id = templateId,
                    page = "pages/index/index",
                    form_id = prepayId,
                    data = data
                };
                PostTemplateMessage(accessToken, message);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }

        public static void PostSendMsgSuccess(string accessToken, string openId, string templateId, string formId,
            TemplateData data)
        {
            try
            {
                var message = new TemplateMessage
                {
                    touser = openId,
                    template_id = templateId,
                    page = "pages/index/index",
                    form_id = formId,
                    data = data
                };
                PostTemplateMessage(accessToken, message);
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                throw;
            }
        }
    }
}
